package com.red.wood.module.cdn.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.red.wood.common.domain.Page;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.common.util.IMap;
import com.red.wood.module.cdn.dao.CdnIpInfoLinkDao;

@Repository
public class CdnIpInfoLinkDaoImpl implements CdnIpInfoLinkDao {

	private static final Log log = LogFactory.getLog(CdnDataDaoImpl.class);
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public Map<String, Object> getPageData(IMap param) {
		String count = "0";
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		list = listPageData(param);
		count = listCount(param);
		
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("totalCount", count);
		rs.put("pageData", list);
		return rs;
	}
	
	public List<Map<String, Object>> listPageData(IMap param){
		String linkCdnNetId = CommonUtils.valueOf(param.get("linkCdnNetId"));
		Page page = (Page) param.get("page");
		int offset = (page.getPageNo() - 1) * page.getPageSize();
		StringBuffer sql = new StringBuffer();
		sql.append("select (select cdn.cdnName from t_cdn_cdndata cdn where cdn.cdnId = ip.cdnId) as cdnName,ip.startIp,ip.endIp,'ipv4' as ipType,(inet_aton(ip.endIp) - inet_aton(ip.startIp) + 1) as ipNum ");
		sql.append("  from t_cdn_cdnNet_node_house_ipSeg ip where 1 = 1");
		sql.append(getQueryConf(param));
		if(StringUtils.isNotBlank(linkCdnNetId)) {//子网页面下钻过来
			sql.append(" group by ip.cdnId,ip.cdnNetId,ip.startIp,ip.endIp");
		}else {
			sql.append(" group by ip.cdnId,ip.startIp,ip.endIp");
		}
		sql.append(" limit " + page.getPageSize() + " offset " + offset+ " ;");
		log.info(sql);
		return jdbcTemplate.queryForList(sql.toString());
	}
	
	public String listCount(IMap param) {
		String linkCdnNetId = CommonUtils.valueOf(param.get("linkCdnNetId"));
		StringBuffer sql = new StringBuffer();
		sql.append("select count(1) count from(");
		sql.append(" select 1 from t_cdn_cdnNet_node_house_ipSeg ip where 1 = 1");
		sql.append(getQueryConf(param));
		if(StringUtils.isNotBlank(linkCdnNetId)) {//子网页面下钻过来
			sql.append(" group by ip.cdnId,ip.cdnNetId,ip.startIp,ip.endIp");
		}else {
			sql.append(" group by ip.cdnId,ip.startIp,ip.endIp");
		}
		sql.append(") t");
		Map<String, Object> map = jdbcTemplate.queryForList(sql.toString()).get(0);
		String count = map.get("count").toString();
		return count;
	}
	
	
	public StringBuffer getQueryConf(IMap param) {
		String startIp = CommonUtils.valueOf(param.get("startIp"));
		String endIp = CommonUtils.valueOf(param.get("endIp"));
		String linkCdnId = CommonUtils.valueOf(param.get("linkCdnId"));
		String linkCdnNetId = CommonUtils.valueOf(param.get("linkCdnNetId"));
		StringBuffer queryConf = new StringBuffer();
		
		if(StringUtils.isNotBlank(linkCdnNetId)) {
			queryConf.append(" and ip.cdnNetId='"+linkCdnNetId+"'");
		}
		if(StringUtils.isNotBlank(startIp)) {
			queryConf.append(" and ip.startIp like '%"+startIp+"%'");
		}
		if(StringUtils.isNotBlank(endIp)) {
			queryConf.append(" and ip.endIp like '%"+endIp+"%'");
		}
		if(StringUtils.isNotBlank(linkCdnId)) {
			queryConf.append(" and ip.cdnId='"+linkCdnId+"'");
		}
		return queryConf;
	}
}
